<?php declare(strict_types=1);


namespace PinWuYun\domain\service\starLinkServer;

use GuzzleHttp\Exception\GuzzleException;

/**
 * Created: by zhiJie
 * Date: 2021/6/24
 */
class StarDataServer
{
    protected $starGoodsService;

    // 初始化类
    public function __construct()
    {
        $this->starGoodsService = new Goods();
    }

    /**
     * 获取所有spuId
     * @throws GuzzleException
     */
    public function getAllSpuId(): array
    {
        $starGoodsService = new Goods();
        $pageIndex = 1;
        $temp = json_decode($starGoodsService->getSpuIdList([
            'pageIndex' => $pageIndex,
        ])->getBody()->getContents(),true)['data'];
        $spuIdList = $temp['spuIdList'];

        $pages = $temp['pages'];
        while($pageIndex < $pages) {
            $pageIndex++;
            $tmp = json_decode($starGoodsService->getSpuIdList([
                'pageIndex' => $pageIndex,
            ])->getBody()->getContents(),true)['data'];
            $spuIdList  = array_merge($tmp['spuIdList'], $spuIdList);
        }
        return $spuIdList;
    }

    /**
     * 传入一个spuId,获取spu下所SKU、价格、库存
     * @throws GuzzleException
     */
    public function getAllInfoWithSpuId(string $spuId)
    {
        /** 获取所有sku的code */
        $skuList = json_decode($this->starGoodsService->getSkuListDetailBySpuId(['spuId' => $spuId])->getBody()->getContents(), true)['data'];
        $codes = array_column($skuList,'code');
        /** 如果是一个，获取第一个，如果是多个，分逗号分割 */
        $codes = (count($codes) === 1 ? $codes[0] : implode(',', $codes));
        /** 获取sku库存和价格 */
        $inventory = json_decode($this->starGoodsService->getSkuInventory(['codes' => $codes])->getBody()->getContents(), true)['data']['skuInvList'];
        $salePrice = json_decode($this->starGoodsService->getSkuSalePrice(['codes' => $codes])->getBody()->getContents(), true)['data'];
        /** 批量匹配 不严谨，但是目前来说顺序不会出错 */
        foreach ($skuList as $index => $skuInfo) {
            $skuList[$index]['inventory'] = $inventory[$index];
            $skuList[$index]['salePrice'] = $salePrice[$index];
        }
        return $skuList;
    }

    /**
     * 昨天到当前时间点的全部商品变更记录
     * @throws GuzzleException
     */
    public function getAllChangeRecord(): array
    {
        $begin = date("Y-m-d", strtotime("-1 day"))." 0:0:0";
        $end = date('Y-m-d H:i:s', time());
        return $this->getRecordByType( $begin, $end);

    }

    /**
     * 获取单个类型的表更记录
     * @param string $begin
     * @param string $end
     * @param int $state
     * @return array|mixed
     * @throws GuzzleException
     */
    private function getRecordByType(string $begin, string $end, int $state=124)
    {
        $starGoodsService = new Goods();
        $pageIndex = 1;
        $requestParam = [
            'changeDateStart'=>$begin,
            'changeDateEnd'=>$end,
            'pageIndex'=>$pageIndex,
        ];
        if($state !== 124) {
            $requestParam['recordType'] = $state;
        }
        // 获取变更记录
        $temp = json_decode(
            $starGoodsService->queryProductChangeRecord($requestParam
            )->getBody()->getContents(),true)['data'];
        $record = $temp['list'];
        $pages = $temp['pages'];
        // 如果有多页，继续遍历
        while($pageIndex < $pages) {
            $pageIndex++;
            $tmp =json_decode($starGoodsService->queryProductChangeRecord($requestParam)
                ->getBody()->getContents(),true)['data'];
            $record = array_merge($tmp['list'], $record);
        }
        return $record;
    }


}